﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "u_mainpage.h"
#include <QSerialPortInfo>
#include <QPalette>
#include <QDebug>
#include <QPainter>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->parent = parent;
    globalData = GlobalData::GetInstance();
    ui->setupUi(this);
    this->InitWidget();
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    this->setAttribute(Qt::WA_QuitOnClose);
    this->setObjectName("mainWindow");
    this->centralWidget()->layout()->setMargin(0);
    this->centralWidget()->layout()->setSpacing(0);
    QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        m_serialPortName << info.portName();
        qDebug()<<"serialPortName:"<<info.portName();
    }
    ui->centralwidget->setMouseTracking(true);
    ui->labelZY_1->setRange(16,0.5,2);
    ui->labelZY_2->setRange(16,0.5,2);
    //this->ClearData();
    this->UpdataData();
    this->SetupConnections();
    this->Initialize();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitWidget(){
    ui->btnON_1->installEventFilter(this);
    ui->btnON_2->installEventFilter(this);
    ui->btnON_3->installEventFilter(this);
    ui->btnON_4->installEventFilter(this);
    ui->btnON_1->setEnabled(false);
    ui->btnON_2->setEnabled(false);
    ui->btnON_3->setEnabled(false);
    ui->btnON_4->setEnabled(false);
    ui->btnOFF_1->installEventFilter(this);
    ui->btnOFF_2->installEventFilter(this);
    ui->btnOFF_3->installEventFilter(this);
    ui->btnOFF_4->installEventFilter(this);
    //ui->lineEditIn_1->setFocusPolicy(Qt::NoFocus);    //去除焦点 不会有边框
    ui->labJE->setStyleSheet(GREEN);
    ui->labJS->setStyleSheet(WHITE);
    ui->labZE->setStyleSheet(GREEN);
    ui->labZS->setStyleSheet(WHITE);
    ui->proIn_1->setStyleSheet(PRO);
    ui->proIn_2->setStyleSheet(PRO);
    ui->proIn_3->setStyleSheet(PRO);
    ui->proIn_4->setStyleSheet(PRO);
    ui->proRef_1->setStyleSheet(PRO);
    ui->proRef_2->setStyleSheet(PRO);
    ui->proRef_3->setStyleSheet(PRO);
    ui->proRef_4->setStyleSheet(PRO);
    //ui->btnJX_1->setFixedWidth(10);//设置宽度
//    ui->btnJX_1->setToolTip("江西一套1号数据");
//    ui->btnJX_2->setToolTip("江西一套2号数据");
//    ui->btnZY_1->setToolTip("中央一套1号数据");
//    ui->btnZY_2->setToolTip("中央一套2号数据");
    ui->widget_6->setStyleSheet(FONT);
    ui->widget_6->setAlignment(Qt::AlignHCenter);
    ui->widget_24->setStyleSheet(FONT);
    ui->widget_24->setAlignment(Qt::AlignHCenter);
    ui->widget_25->setStyleSheet(FONT);
    ui->widget_25->setAlignment(Qt::AlignHCenter);
    ui->widget_26->setStyleSheet(FONT);
    ui->widget_26->setAlignment(Qt::AlignHCenter);

}

void MainWindow::SetupConnections(){
    //connect(ui->widget_2, SIGNAL(ShowPage(int)), stackedWidget, SLOT(setCurrentIndex(int)));
    ///更新四个仪表盘指针///
    connect(this,SIGNAL(updatePoint(float)),ui->LabelJX_1,SLOT(setCurentSpeed(float)));
    connect(this,SIGNAL(updatePoint_2(float)),ui->labelJX_2,SLOT(setCurentSpeed(float)));
    connect(this,SIGNAL(updatePoint_3(float)),ui->labelZY_1,SLOT(setCurentSpeed(float)));
    connect(this,SIGNAL(updatePoint_4(float)),ui->labelZY_2,SLOT(setCurentSpeed(float)));
    ///天线设置成功时，更新天线指向和参数///
    connect(globalData,SIGNAL(updateantenna1(QString,bool,bool)),ui->widJX,SLOT(updateLine(QString,bool,bool)));
    connect(globalData,SIGNAL(updateantenna2(QString,bool,bool)),ui->widZY,SLOT(updateLine(QString,bool,bool)));
    ///天线设置失败时，恢复天线参数///
    connect(globalData,SIGNAL(DefeatedJX()),ui->widJX,SLOT(defeatedJX()));
    connect(globalData,SIGNAL(DefeatedZY()),ui->widZY,SLOT(defeatedZY()));
    ///实时数据///
    connect(ui->btnJX_1,&QPushButton::clicked,this,&MainWindow::showWidget_JX1);
    connect(ui->btnJX_2,&QPushButton::clicked,this,&MainWindow::showWidget_JX2);
    connect(ui->btnZY_1,&QPushButton::clicked,this,&MainWindow::showwidget_Zy1);
    connect(ui->btnZY_2,&QPushButton::clicked,this,&MainWindow::showWidget_ZY2);
    ///工作塔指示灯///
    connect(ui->widJX,SIGNAL(Light(int)),this,SLOT(LightChange(int)));
    connect(ui->widZY,SIGNAL(Light(int)),this,SLOT(LightChange1(int)));
    ///实时数据状态是否可点击///
    connect(ui->widJX,SIGNAL(work(bool)),this,SLOT(workState(bool)));
    connect(ui->widZY,SIGNAL(work(bool)),this,SLOT(workState1(bool)));
}

void MainWindow::Initialize(){

}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
//    startPos = event->globalPos();//点击时的全局位置
//    startPos1= this->pos();//窗口左上角坐标
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
//    if(event->buttons() & Qt::LeftButton)
//    {
//        QPoint movePos=event->globalPos() - startPos;
//        if(movePos.manhattanLength()>4)
//           { QPoint point1=startPos-startPos1;
//            this->move(event->globalPos() - point1);}
//        return;
//    }
}


//主窗体设置FramelessWindowHint后，窗体接收paint消息异常  app不刷新
void MainWindow::showEvent(QShowEvent* event){
    this->setAttribute(Qt::WA_Mapped);//小部件映射在屏幕上
    QWidget::showEvent(event);
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    QPen pen(Qt::gray);
    pen.setWidth(2);
    p.setPen(pen);
    p.drawLine(0,0,this->width(),0);
    p.drawLine(0,0,0,this->height());
    p.drawLine(this->width(),0,this->width(),this->height());
    p.drawLine(0,this->height(),this->width(),this->height());
}

void MainWindow::UpdataData()
{
    timer=new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(showData()));
    connect(timer,SIGNAL(timeout()),this,SLOT(showData_JX()));
    connect(timer,SIGNAL(timeout()),this,SLOT(showData_ZY1()));
    connect(timer,SIGNAL(timeout()),this,SLOT(showData_ZY2()));
    timer->start(200);
}

void MainWindow::ClearData()
{
    timer1=new QTimer();
    connect(timer1,SIGNAL(timeout()),this,SLOT(clearData()));
    timer1->start(100);
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{

//    portJX_2=globalData->serList.at(1);
//    portZY_1=globalData->serList.at(2);
//    portZY_2=globalData->serList.at(3);
    if(watched==ui->btnON_1)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {   ui->btnON_1->setEnabled(false);//可用
            ui->btnOFF_1->setEnabled(true);

            int index = globalData->GetEquIndexByCode("001");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD8);
                connect(timer,SIGNAL(timeout()),this,SLOT(showData()));//重新连接数据显示connect
            }

            off_1=false;
            emit globalData->seriPort();
        }
    }
    if(watched==ui->btnOFF_1)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            ui->btnOFF_1->setEnabled(false);
            ui->btnON_1->setEnabled(true);

            int index = globalData->GetEquIndexByCode("001");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD5);
                disconnect(timer,SIGNAL(timeout()),this,SLOT(showData()));
            }

            ui->label->setText(QString::number(0,'f',1));
            ui->label_3->setText(QString::number(0,'f',1));
            ui->proIn_1->setValue(0);
            ui->proRef_1->setValue(0);
            ui->labelLight_1->setStyleSheet(WHITE);
            off_1=true;
            emit updatePoint(0);
            emit globalData->seriPort();
        }
    }
    if(watched==ui->btnON_2)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            ui->btnON_2->setEnabled(false);
            ui->btnOFF_2->setEnabled(true);

            int index = globalData->GetEquIndexByCode("002");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD8);
                connect(timer,SIGNAL(timeout()),this,SLOT(showData_JX()));
            }

            off_2=false;
            //timer->start(100);
            emit globalData->seriPort();
        }
    }
    if(watched==ui->btnOFF_2)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            ui->btnOFF_2->setEnabled(false);
            ui->btnON_2->setEnabled(true);

            int index = globalData->GetEquIndexByCode("002");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD5);
                disconnect(timer,SIGNAL(timeout()),this,SLOT(showData_JX()));
            }

            ui->labIn_2->setText(QString::number(0,'f',1));
            ui->labRef_2->setText(QString::number(0,'f',1));
            ui->proIn_2->setValue(0);
            ui->proRef_2->setValue(0);
            ui->labelLight_2->setStyleSheet(WHITE);
            off_2=true;
            emit updatePoint_2(0);
            emit globalData->seriPort();
        }
    }
    if(watched==ui->btnON_3)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            ui->btnON_3->setEnabled(false);
            ui->btnOFF_3->setEnabled(true);

            int index = globalData->GetEquIndexByCode("003");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD8);
                connect(timer,SIGNAL(timeout()),this,SLOT(showData_ZY1()));
            }

            off_3=false;
            emit globalData->seriPort();
        }
    }
    if(watched==ui->btnOFF_3)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            ui->btnON_3->setEnabled(true);
            ui->btnOFF_3->setEnabled(false);

            int index = globalData->GetEquIndexByCode("003");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD5);
                disconnect(timer,SIGNAL(timeout()),this,SLOT(showData_ZY1()));
            }

            ui->labIn_3->setText(QString::number(0,'f',1));
            ui->labRef_3->setText(QString::number(0,'f',1));
            ui->proIn_3->setValue(0);
            ui->proRef_3->setValue(0);
            ui->labelLight_3->setStyleSheet(WHITE);
            off_3=true;
            emit updatePoint_3(0);
            emit globalData->seriPort();
        }
    }
    if(watched==ui->btnON_4)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            off_4=false;
            ui->btnON_4->setEnabled(false);
            ui->btnOFF_4->setEnabled(true);

            int index = globalData->GetEquIndexByCode("004");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD8);
                connect(timer,SIGNAL(timeout()),this,SLOT(showData_ZY2()));
            }
        }
    }
    if(watched==ui->btnOFF_4)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            ui->btnON_4->setEnabled(true);
            ui->btnOFF_4->setEnabled(false);

            int index = globalData->GetEquIndexByCode("004");
            if(index >= 0){
                //globalData->taskManager->receiverList.at(index)->SetCmd(CMD5);
                disconnect(timer,SIGNAL(timeout()),this,SLOT(showData_ZY2()));
            }

            ui->label_4->setText(QString::number(0,'f',1));
            ui->label_5->setText(QString::number(0,'f',1));
            ui->proIn_4->setValue(0);
            ui->proRef_4->setValue(0);
            ui->labelLight_4->setStyleSheet(WHITE);
            off_4=true;
            emit updatePoint_4(0);
        }
    }
    return QMainWindow::eventFilter(watched,event);     // 最后将事件交给上层对话框
}

void MainWindow::showData()
{ 
    Simulation *sim0 = globalData->GetSimByCode("001");
    QString launch0 =QString::number(sim0->launch,'f',1);
    QString reflect0=QString::number(sim0->reflect,'f',1);
    ui->label->setText(launch0);
    ui->label_3->setText(reflect0);
    ui->proIn_1->setValue(launch0.toFloat());
    ui->proRef_1->setValue(reflect0.toFloat());

    QString name;
    name=sim0->devname;
    QString hh="\n";
    name.append(hh);
    name = name + QString::number(sim0->fre);
    ui->widget_6->setText(name+"KHz"+"(50Kw)");
    if(off_1)
    {
        ui->labelLight_1->setStyleSheet(WHITE);
    }
    else if((!globalData->Light_1)&&(!off_1))
    {
        ui->labelLight_1->setStyleSheet(twink? RED : WHITE);
        twink=!twink;
    }
    else if(globalData->Light_1&&(!off_1))
        ui->labelLight_1->setStyleSheet(GREEN);
    emit updatePoint(sim0->launch);
}

void MainWindow::showData_JX()
{
    Simulation *sim1 = globalData->GetSimByCode("002");
    QString launch1 =QString::number(sim1->launch,'f',1);
    QString reflect1=QString::number(sim1->reflect,'f',1);
    ui->labIn_2->setText(launch1);
    ui->labRef_2->setText(reflect1);
    ui->proIn_2->setValue(launch1.toFloat());
    ui->proRef_2->setValue(reflect1.toFloat());

    QString name;
    name=sim1->devname;
    QString hh="\n";
    name.append(hh);
    name = name + QString::number(sim1->fre);
    ui->widget_24->setText(name+"KHz"+"(50Kw)");

    if(off_2)
    {
        ui->labelLight_2->setStyleSheet(WHITE);
    }
    else if((!globalData->Light_2)&&(!off_2))
    {
        ui->labelLight_2->setStyleSheet(twink? RED : WHITE);
    }
    else if(globalData->Light_2&&(!off_2))
       { ui->labelLight_2->setStyleSheet(GREEN);}
    emit updatePoint_2(sim1->launch);
}

void MainWindow::showData_ZY1()
{
    Simulation *sim2 = globalData->GetSimByCode("003");
    QString launch2 =QString::number(sim2->launch,'f',1);
    QString reflect2=QString::number(sim2->reflect,'f',1);
    ui->labIn_3->setText(launch2);
    ui->labRef_3->setText(reflect2);
    ui->proIn_3->setValue(launch2.toFloat());
    ui->proRef_3->setValue(reflect2.toFloat());

    QString name;
    name=sim2->devname;
    QString hh="\n";
    name.append(hh);
    name = name + QString::number(sim2->fre);
    ui->widget_25->setText(name+"KHz"+"(10Kw)");

    if(off_3)
    {
        ui->labelLight_3->setStyleSheet(WHITE);
    }
    else if((!globalData->Light_3)&&(!off_3))
    {
        ui->labelLight_3->setStyleSheet(twink? RED : WHITE);
    }
    else if(globalData->Light_3 && (!off_3))
        ui->labelLight_3->setStyleSheet(GREEN);
    emit updatePoint_3(sim2->launch);
}

void MainWindow::showData_ZY2()
{
    Simulation *sim3 = globalData->GetSimByCode("004");
    QString launch3 =QString::number(sim3->launch,'f',1);
    QString reflect3=QString::number(sim3->reflect,'f',1);
    ui->label_4->setText(launch3);
    ui->label_5->setText(reflect3);
    ui->proIn_4->setValue(launch3.toFloat());
    ui->proRef_4->setValue(reflect3.toFloat());

    QString name;
    name=sim3->devname;
    QString hh="\n";
    name.append(hh);
    name = name + QString::number(sim3->fre);
    ui->widget_26->setText(name+"KHz"+"(10Kw)");

    if(off_4)
    {
        ui->labelLight_4->setStyleSheet(WHITE);
    }
    else if((!globalData->Light_4)&&(!off_4))
    {
        ui->labelLight_4->setStyleSheet(twink? RED : WHITE);
    }
    else if(globalData->Light_4&&(!off_4))
        ui->labelLight_4->setStyleSheet(GREEN);
    emit updatePoint_4(sim3->launch);
}

void MainWindow::clearData()
{
//    ui->lineEditIn_1->setText(QString::number(0,'f',1));
//    ui->lineEditRef_1->setText(QString::number(0,'f',1));
//    ui->lineEditIn_2->setText(QString::number(0,'f',1));
//    ui->lineEditRef_2->setText(QString::number(0,'f',1));
//    ui->lineEditIn_3->setText(QString::number(0,'f',1));
//    ui->lineEditRef_3->setText(QString::number(0,'f',1));
//    ui->lineEditIn_4->setText(QString::number(0,'f',1));
//    ui->lineEditRef_4->setText(QString::number(0,'f',1));
}

void MainWindow::showWidget_JX1()
{
(new RealTimeData("001",this->parent))->exec();
}

void MainWindow::showWidget_JX2()
{
(new RealTimeData("002",this->parent))->exec();
}

void MainWindow::showwidget_Zy1()
{
(new RealTimeData("003",this->parent))->exec();
}

void MainWindow::showWidget_ZY2()
{
    (new RealTimeData("004",this->parent))->exec();
}

void MainWindow::LightChange(int a)
{
    int lig=a;
    if(lig==2)
    {
        ui->labJE->setStyleSheet(GREEN);
        ui->labJS->setStyleSheet(WHITE);
    }
    else if(lig==1)
    {
        ui->labJE->setStyleSheet(WHITE);
        ui->labJS->setStyleSheet(GREEN);
    }
}

void MainWindow::LightChange1(int a)
{
    int lig=a;
    if(lig==2)
    {
        ui->labZE->setStyleSheet(GREEN);
        ui->labZS->setStyleSheet(WHITE);
    }
    else if(lig==1)
    {
        ui->labZE->setStyleSheet(WHITE);
        ui->labZS->setStyleSheet(GREEN);
    }
}

void MainWindow::workState(bool a)
{
//    ui->btnJX_1->setEnabled(a);
//    ui->btnJX_2->setEnabled(!a);
}

void MainWindow::workState1(bool a)
{
//    ui->btnZY_1->setEnabled(a);
//    ui->btnZY_2->setEnabled(!a);
}



